package com.ftrend.printdemo.common;

import android.content.Context;
import android.hardware.usb.UsbDevice;

import com.loopstack.gprint.io.PrinterDevices;
import com.loopstack.gprint.utils.Command;
import com.loopstack.gprint.utils.ConnMethod;


public class PrinterManager {
   static   PrinterManager printerManager=null;
    CallbackListener callbackListener=null;
    /**
     * 单例
     * @return
     */
    public static PrinterManager getInstance(){
        if (printerManager==null){
            printerManager=new PrinterManager();
        }
        return printerManager;
    }
    public void setCallbackListener(CallbackListener callbackListener){
        this.callbackListener=callbackListener;
    }

    /**
     * 连接
     * @return
     */
    public void connectBlueTooth(Context mContext , String macAddress)
    {
                 PrinterDevices blueTooth = new PrinterDevices.Build()
                         .setContext(mContext)
                         .setConnMethod(ConnMethod.BLUETOOTH)
                         .setMacAddress(macAddress)
                         .setCallbackListener(new com.loopstack.gprint.utils.CallbackListener() {
                             @Override
                             public void onConnecting() {
                                 if (callbackListener!=null){
                                     callbackListener.onConnecting(PrintType.BLUE);
                                 }
                             }

                             @Override
                             public void onCheckCommand() {

                             }

                             @Override
                             public void onSuccess(PrinterDevices printerDevices) {
                                 if (callbackListener!=null){

//                                     SharedPreferences sharedPreferences = SharedPreferencesUtil.getSharedPreferences(mContext);
//                                     String printerName = sharedPreferences.getString("printerName", "");
//                                     Log.e("aoto",printerName);
//                                     if (printerName.equals("XP-T202UA"))
//                                     {
//                                         PrinterBlue.setPrinterCommand(Command.TSC);
//                                         SharedPreferencesUtil.putBoolean(mContext, BaseConst.SHP_KEY_IS_GP_PRINT,false);
//                                     }else {
//                                         SharedPreferencesUtil.putBoolean(mContext, BaseConst.SHP_KEY_IS_GP_PRINT,true);
//                                     }
//
//                                     if (printerName.equals("MHT-P58D")) {
//                                         PrinterBlue.setPrinterCommand(Command.ESC);
//                                     }
//                                     if (printerName.equals("MHT-L58G")) {
//                                         PrinterBlue.setPrinterCommand(Command.TSC);
//                                     }
                                     callbackListener.onSuccess(PrintType.BLUE);
                                 }
                             }

                             @Override
                             public void onReceive(byte[] bytes) {

                             }

                             @Override
                             public void onFailure() {
//                                 SharedPreferences sharedPreferences = SharedPreferencesUtil.getSharedPreferences(mContext);
//                                 String printerName = sharedPreferences.getString("printerName", "");
//                                 if (printerName.equals("MHT-P58D")) {
//                                     PrinterBlue.setPrinterCommand(Command.ESC);
//                                     if (callbackListener!=null) {
//                                         callbackListener.onSuccess(PrintType.BLUE);
//                                     }
//                                 } else if (printerName.equals("MHT-L58G")) {
//                                     PrinterBlue.setPrinterCommand(Command.TSC);
//                                     if (callbackListener!=null) {
//                                         callbackListener.onSuccess(PrintType.BLUE);
//                                     }
//                                 }else {
//                                     if (callbackListener!=null){
//                                         callbackListener.onFailure(PrintType.BLUE);
//                                     }
//                                 }
                             }

                             @Override
                             public void onDisconnect() {
                                 if (callbackListener!=null){
                                     callbackListener.onDisconnect(PrintType.BLUE);
                                 }
                             }
                         })
                         .build();
                 PrinterBlue.connect(blueTooth,mContext);

    }
    /**
     * 蓝牙连接
     * @return
     */
    public void connectUsb(Context mContext,PrintType printType, UsbDevice usbDevice)
    {
        if (usbDevice==null)
        {
            return;
        }
        switch (printType){

            case BAR_USB:{
                PrinterDevices usb=new PrinterDevices.Build()
                        .setContext(mContext)
                        .setConnMethod(ConnMethod.USB)
                        .setUsbDevice(usbDevice)
                        .setCommand(Command.TSC)
                        .setCallbackListener(new com.loopstack.gprint.utils.CallbackListener() {
                            @Override
                            public void onConnecting() {
                                if (callbackListener!=null){
                                    callbackListener.onConnecting(PrintType.BAR_USB);
                                }
                            }

                            @Override
                            public void onCheckCommand() {

                            }

                            @Override
                            public void onSuccess(PrinterDevices printerDevices) {
                                if (callbackListener!=null){
                                    callbackListener.onSuccess(PrintType.BAR_USB);
                                }
                            }

                            @Override
                            public void onReceive(byte[] bytes) {

                            }

                            @Override
                            public void onFailure() {
                                if (callbackListener!=null){
                                    callbackListener.onFailure(PrintType.BAR_USB);
                                }
                            }

                            @Override
                            public void onDisconnect() {
                                if (callbackListener!=null){
                                    callbackListener.onDisconnect(PrintType.BAR_USB);
                                }
                            }
                        })
                        .build();
                PrinterBarUsb.getInstance().connect(usb,mContext);
            }

                break;
            case TICKET_USB:
            {
                PrinterDevices usb=new PrinterDevices.Build()
                        .setContext(mContext)
                        .setConnMethod(ConnMethod.USB)
                        .setUsbDevice(usbDevice)
                        .setCommand(Command.ESC)
                        .setCallbackListener(new com.loopstack.gprint.utils.CallbackListener() {
                            @Override
                            public void onConnecting() {
                                if (callbackListener!=null){
                                    callbackListener.onConnecting(PrintType.TICKET_USB);
                                }
                            }

                            @Override
                            public void onCheckCommand() {

                            }

                            @Override
                            public void onSuccess(PrinterDevices printerDevices) {
                                if (callbackListener!=null){
                                    callbackListener.onSuccess(PrintType.TICKET_USB);
                                }
                            }

                            @Override
                            public void onReceive(byte[] bytes) {

                            }

                            @Override
                            public void onFailure() {
                                if (callbackListener!=null){
                                    callbackListener.onFailure(PrintType.TICKET_USB);
                                }
                            }

                            @Override
                            public void onDisconnect() {
                                if (callbackListener!=null){
                                    callbackListener.onDisconnect(PrintType.TICKET_USB);
                                }
                            }
                        })
                        .build();
                PrinterTicketUsb.getInstance().connect(usb);
            }
                break;
        }
    }
    /**
     * 获取连接状态
     * @return
     */
    public boolean isConnect(PrintType printType){

       switch (printType){
           case TICKET_BLUE:
           case BAR_BLUE:
            return    PrinterBlue.getConnectState();

           case BAR_USB:
             return   PrinterBarUsb.getConnectState();

           case TICKET_USB:
            return    PrinterTicketUsb.getConnectState();

       }
        return false;
   }
    /**
     * 关闭连接
     * @return
     */
    public void disConnect(PrintType printType){

        switch (printType){
            case BLUE:
                    PrinterBlue.close();
           break;
            case BAR_USB:
                   PrinterBarUsb.close();
            break;
            case TICKET_USB:
                    PrinterTicketUsb.close();
            break;
        }

    }
    public void closeConnect(){
        try {
            PrinterBlue.close();
            PrinterBarUsb.close();
            PrinterTicketUsb.close();
            printerManager=null;
            callbackListener=null;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    //小票机是否连接
    public static boolean printTicketIsConnect(){
        boolean isConnect=(PrinterBlue.getPortManager() == null ||
                !PrinterBlue.getConnectState() || PrinterBlue.getPrinterCommand() != Command.ESC) && (PrinterTicketUsb.getPortManager() == null ||
                !PrinterTicketUsb.getConnectState() || PrinterTicketUsb.getPrinterCommand() != Command.ESC);
        return isConnect;
    }

    //标签机是否连接
    public static boolean printBarIsConnect(){
        boolean isConnect=(PrinterBlue.getPortManager() == null ||
                !PrinterBlue.getConnectState()||PrinterBlue.getPrinterCommand() != Command.TSC)&&(PrinterBarUsb.getPortManager() == null ||
                !PrinterBarUsb.getConnectState()||PrinterBarUsb.getPrinterCommand() != Command.TSC);
        return isConnect;
    }

    public interface CallbackListener {
        void onConnecting(PrintType printType);

        void onSuccess(PrintType printType);

        void onFailure(PrintType printType);

        void onDisconnect(PrintType printType);
    }
}
